package com.oauth2.springsecurityoauth2demo.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;


/**
 * @author wangning
 * @create 2021-03-30 20:26
 */
@RestController
@RequestMapping("/user")
public class UserController {

	/**
	 * 获取当前用户对象
	 * @param authentication
	 * @return
	 */
	@RequestMapping("/getCurrentUser")
	public Object getCurrentUser(Authentication authentication, HttpServletRequest request) {
		String header = request.getHeader("Authorization");
		String token = header.substring(header.indexOf("bearer") + 7);
		System.out.println("token = " + token);
		Claims body = Jwts.parser().setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
				.parseClaimsJws(token).getBody();

		System.out.println("body = " + body);
//		return authentication.getPrincipal();
		return body;
	}
}
